--
-- Author: ping.wang
-- Date: 2018-07-11 14:03:16
--
-- AuctionBidBuyRender
--

local ClassRef = app.mvc.createRenderClass(...)
local string_format = string.format
local math_floor = math.floor
local auctionAdjustPriceRate = GD:getSystemPar( "auctionAdjustPriceRate",1)

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionBuy2Render))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)
    self._bidPrice = 0


    self._curMaxPrice = self._ui.Panel_num:findChild("num") --当前最高出价
    self._fixedPrice = self._ui.Panel_num_fixed:findChild("num") --一口价
    self._myGoldNum = self._ui.Panel_num_my:findChild("num") --我所拥有的金币
    self._myGoldIcon = self._ui.Panel_num_my:findChild("gold")

    self._ui.btn_offer:addClickEventListener(function()
        local goodData = self._data.goodData
        if goodData then
            --出价
            executeFunc(self._onClick, "bidBuy", {goodData,self._bidPrice})
        end
    end)


    self._ui.btn_buy:addClickEventListener(function()
        local goodData = self._data.goodData
        if goodData then
            --一口价
            local fixedPrice = goodData:getFixedPrice()
            executeFunc(self._onClick, "fixedBuy", {goodData:getProductUUId(),fixedPrice})
        end
    end)

    self._ui.Panel_supplies:addClickEventListener(function() executeFunc(self._onClick, "showMiniKeyboard") end)
    local minusBtn = self._ui.Panel_supplies:findChild("Button_minus")
    minusBtn:addTouchEventListener(handler( self, self._minusBtnClickEvent ) )
    local plusBtn = self._ui.Panel_supplies:findChild("Button_plus")
    plusBtn:addTouchEventListener(handler( self, self._plusBtnClickEvent ) )
    self._bidPriceText = self._ui.Panel_supplies:findChild("num/num") --出价

    ----------小键盘start--------------
    local suppliesPanelBg = self._ui.Panel_supplies:findChild("bg")
    display.uiAddClick( suppliesPanelBg, function()
        local btn = self._ui.Panel_supplies:findChild("Button_plus")
        local posTemp = btn:convertToWorldSpace(cc.p(0,0))
        local width, height = btn:getContentSize2()
        posTemp.x = posTemp.x + width

        executeFunc(self._onClick, "showMiniKeyboard", {posTemp,self._bidPrice,handler( self, self.setAndGetBidPrice )} )
    end )
    ----------小键盘end--------------

    self._originalBidingPrice = 0

    self:setContentSize( self._ui.Panel:getContentSize() )

    --设置货币icon
    local goldIcon1 = self._ui.Panel_num:findChild("gold")
    local goldIcon2 = self._ui.Panel_num_fixed:findChild("gold")
    local goldIcon3 = self._ui.Panel_num_my:findChild("gold")
    local goldIcon4 = self._ui.Panel_supplies:findChild("num/gold")


    local coinId = 99
    local publisher = app:getInnerPublisher()
    if publisher == Enums.Publisher.tencent then
        coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType_tencent",99))
    else
        coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType",115))
    end
    self._coinId = coinId
    self._coinConfig = GD:getCoinConfig(self._coinId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,self._coinConfig.bigicon)
    goldIcon1:loadTexture(iconPath)
    goldIcon2:loadTexture(iconPath)
    goldIcon3:loadTexture(iconPath)
    goldIcon4:loadTexture(iconPath)

    self:refresh()
end

function ClassRef:refresh()
    if self._data and self._data.goodData then
        self:setVisible( true )

        local goodData = self._data.goodData
        self._myGoldNum:setString(string_format(L("auction_goldNum"),L(self._coinConfig.name),UD:getDaibiValue(self._coinId)))
        self:_adjustIconPosX(self._myGoldIcon,self._myGoldNum)

        self._originalBidingPrice = goodData:getBidingPrice() --记录初始的竞标价
        if goodData:hasBiding() then
            self._curMaxPrice:setString(string_format(L("auction_topPrice"),self._originalBidingPrice))
        else
            self._curMaxPrice:setString(string_format(L("auction_startPrice"),self._originalBidingPrice))
        end
        
        self._fixedPrice:setString(string_format(L("auction_donePrice"),goodData:getFixedPrice()))
        --显示出价 最高出价 而且此处出价应该不小于最高出价
        if self._originalBidingPrice == goodData:getSellPrice() and not goodData:hasBiding() then
            self:setAndGetBidPrice(self._originalBidingPrice)
        else
            local val = math_floor(auctionAdjustPriceRate * self._originalBidingPrice/100)
            if val == 0 then
                val = 1
            end
            self:setAndGetBidPrice(self._originalBidingPrice + val)
        end

       
    else
        self:setVisible( false )
    end
end

--基于文本调整ICON的位置
function ClassRef:_adjustIconPosX(icon,text)
    local parentPanelW = text:getParent():getContentSize().width/2
    local fixedGoldenPosX = parentPanelW + text:getContentSize().width/2 + icon:getContentSize().width * 0.6
    icon:setPositionX(fixedGoldenPosX)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

--设置竞标价格
function ClassRef:setAndGetBidPrice(num)
    local goodData = self._data.goodData
    if num > goodData:getFixedPrice() then
        num = goodData:getFixedPrice()
    elseif num <= goodData:getBidingPrice() then
        if self._originalBidingPrice == goodData:getSellPrice() and not goodData:hasBiding() then
             num = goodData:getBidingPrice()
        else
            local val = math_floor(auctionAdjustPriceRate * self._originalBidingPrice/100)
            if val == 0 then
                val = 1
            end
            num = goodData:getBidingPrice() + val
        end
    end

    self._bidPrice = num
    --刷新显示 判断合法性
    self._bidPriceText:setString(string_format(L("auction_pay"),self._bidPrice))
    return self._bidPrice
end

--减少数量
function ClassRef:_minusBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_minusBidPrice() end)
end

--增大数量
function ClassRef:_plusBtnClickEvent(sender,eventType)
    self:_doLongPressClickEvent(sender,eventType,function() self:_plusBidPrice() end)
end

--按比率增加和减少单价 无法手动修改具体的价格
function ClassRef:_minusBidPrice()
    local val = math_floor(auctionAdjustPriceRate * self._originalBidingPrice/100)
    if val == 0 then
        val = 1
    end
    self:setAndGetBidPrice(self._bidPrice - val)
end

function ClassRef:_plusBidPrice()
    local val = math_floor(auctionAdjustPriceRate * self._originalBidingPrice/100)
    if val == 0 then
        val = 1
    end
    self:setAndGetBidPrice(self._bidPrice + val)
end

function ClassRef:_doLongPressClickEvent(sender,eventType,callFunc)
    if eventType == ccui.TouchEventType.began then
        self._delayPressAction = self:performWithDelay(
            function()
                if not self._longPressAction then
                    self._longPressAction = self:schedule(callFunc,0.05)
                end
            end,0.3) --0.3秒后执行回调

    elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then

        if self._delayPressAction then
            self:stopAction(self._delayPressAction)
            self._delayPressAction = nil
        end

        if self._longPressAction then
            self:stopAction(self._longPressAction)
            self._longPressAction = nil
        else
            callFunc()
        end
    end
end

return ClassRef
